layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index', //主入口模块
}).use(['index', 'echarts', 'carousel', 'senior'], function(){
    var $ = layui.$,
        echarts = layui.echarts;

    //初始化统计图表
    getRegisterStatics(1);

    $(document).on('click', '#year-reg', function () {
        getRegisterStatics(2);
    });
    $(document).on('click', '#month-reg', function () {
        getRegisterStatics(1);
    });

    /** =====================数据统计 start======================= */

    /**
     * 渲染统计图表
     * @param type
     */
    function getRegisterStatics(type){
        var data = {'type': type};
        var userInviteNum = new Array();
        var channelInviteNum = new Array();
        var channelNum = new Array();
        var createTime = new Array();
        post('home/channelRegisterStatics', data, false,function (data) {
            var channelRegisterStaticsList = data.channelRegisterStaticsList;
            for(var i in channelRegisterStaticsList){
                var staticsMap = channelRegisterStaticsList[i];
                for(var j in staticsMap){
                    if(j == 'userInviteNum'){
                        userInviteNum.push(staticsMap[j])
                    }else if(j == 'channelInviteNum'){
                        channelInviteNum.push(staticsMap[j])
                    }else if(j == 'channelNum'){
                        channelNum.push(staticsMap[j])
                    }else if(j == 'createTime'){
                        createTime.push(staticsMap[j]);
                    }
                }
            }

            var echnormcol = [], normcol = [{
                    tooltip : {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        }
                    },
                    legend: {data:['邀请用户注册', '渠道邀请注册', '邀请渠道注册']},
                    calculable : true,
                    xAxis : [{
                        type : 'category',
                        data : createTime
                    }],
                    yAxis : [{type:"value"}],
                    series : [
                        {
                            name:'邀请用户注册',
                            type:'bar',
                            stack: '总数',
                            barWidth: 20,
                            data: userInviteNum
                        },
                        {
                            name:'渠道邀请注册',
                            type:'bar',
                            stack: '总数',
                            barWidth: 20,
                            data: channelInviteNum
                        },
                        {
                            name:'邀请渠道注册',
                            type:'bar',
                            stack: '总数',
                            barWidth: 20,
                            data: channelNum
                        }
                    ]
                }],
                elemNormcol = $('#LAY-index-normcol-reg').children('div'),
                renderNormcol = function(index){
                    echnormcol[index] = echarts.init(elemNormcol[index], layui.echartsTheme);
                    echnormcol[index].setOption(normcol[index]);
                    window.onresize = echnormcol[index].resize;
                };
            if(!elemNormcol[0]) return;
            renderNormcol(0);
        });
    }

    /** =====================数据统计 end===================== */

});